package net.csdn.business.common.oauth2.component;

import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2RefreshToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;

import java.util.concurrent.ExecutorService;

/**
 * @Auther: zhangyalei
 * @Date: 2022/12/7 15:57
 * @Description:
 */
@RequiredArgsConstructor
public class OauthAsyncService{

    private final CustomJdbcTokenStore customJdbcTokenStore;

    private final ThreadPoolTaskExecutor taskExecutor;

    public void storeAccessTokenAsync(OAuth2AccessToken oauth2AccessToken, OAuth2Authentication authentication) {
        taskExecutor.execute(()->{
            customJdbcTokenStore.storeAccessToken(oauth2AccessToken,authentication);
        });

    }

    public void storeRefreshTokenAsync(OAuth2RefreshToken refreshToken, OAuth2Authentication authentication) {
        taskExecutor.execute(()->{
                customJdbcTokenStore.storeRefreshToken(refreshToken,authentication);
        });
    }


    public void removeAccessTokenAsync(OAuth2AccessToken accessToken) {
        taskExecutor.execute(()->{
            customJdbcTokenStore.removeAccessToken(accessToken);
        });
    }


    public void removeRefreshTokenAsync(OAuth2RefreshToken refreshToken) {
        taskExecutor.execute(()->{
            customJdbcTokenStore.removeRefreshToken(refreshToken);
        });
    }


    public void removeAccessTokenUsingRefreshTokenAsync(OAuth2RefreshToken refreshToken) {
        taskExecutor.execute(()->{
            customJdbcTokenStore.removeAccessTokenUsingRefreshToken(refreshToken);
        });
    }
}
